package com.yanqu.road.dao.impl.activity.zhounianturntable;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhounianturntable.ZhouNianTurntableData;
import com.yanqu.road.entity.activity.zhounianturntable.ZhouNianTurntableItem;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhouNianTurntableDataDaoImpl extends TempDataObjectDao<ZhouNianTurntableData> {

    public static String getZhouNianTurntableDataSql = "select * from `t_u_activity_zhounian_turntable_data` where `activityId`=? and `userId`=?";

    public ZhouNianTurntableData getZhouNianTurntableData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        ZhouNianTurntableData data = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getZhouNianTurntableDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }


    public static String insertZhouNianTurntableDataSql = "insert into t_u_activity_zhounian_turntable_data(userId,activityId,items,chooseInfoMap) values (?,?,?,?)";
    @Override
    protected boolean addData(ZhouNianTurntableData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getItems())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getChooseInfoMap())));
        if (getDb().executeNoneQuery(insertZhouNianTurntableDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateZhouNianTurntableDataSql = "update `t_u_activity_zhounian_turntable_data` set items=?,chooseInfoMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ZhouNianTurntableData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getItems())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getChooseInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateZhouNianTurntableDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ZhouNianTurntableData parseData(ResultSet rs) throws SQLException {
        ZhouNianTurntableData data = new ZhouNianTurntableData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        String items = rs.getString("items");
        data.setItems(StringUtils.isNullOrEmpty(items) ? new ArrayList<>() : JSONObject.parseObject(items, new TypeReference<List<ZhouNianTurntableItem>>() {
        }));
        String chooseInfoMap = rs.getString("chooseInfoMap");
        data.setChooseInfoMap(StringUtils.isNullOrEmpty(chooseInfoMap) ? new HashMap<>() : JSONObject.parseObject(chooseInfoMap, new TypeReference<Map<String, Integer>>() {
        }));
        return data;
    }
}
